package filter;


import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class BaseBackFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String context = request.getContextPath();
        String uri = request.getRequestURI();
        uri = StringUtils.remove(uri,context);
//        System.out.println(context+" "+uri);
        if(uri.startsWith("/admin_"))
        {
            String class_ = "/"+StringUtils.substringBetween(uri,"_","_")+"Servlet";
//            System.out.println(class_);
            String method = StringUtils.substringAfterLast(uri,"_");
//            System.out.println(method);
            request.setAttribute("method",method);
            request.getRequestDispatcher(class_).forward(request,response);
            return;
        }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
